// 1.安装：npm i vue-router
// 2.引入
import VueRouter from 'vue-router'
import Login from '@/views/login'
import auth from '@/utils/auth'
import Layout from '@/views/Layout'
import Home from '@/views/home'
import Article from '@/views/article'
import Publish from '@/views/publish'

// 3.注册
import Vue from 'vue'
Vue.use(VueRouter)

// 4.初始化
const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      children:
        [
          {
            path: '/',
            component: Home
          },
          {
            path: '/article',
            component: Article
          },
          {
            path: '/publish',
            component: Publish
          }
        ]
    }
  ] // 路由规则
})
// 5.导出路由实例：
export default router
// 6.导入路由实例：main.js
// 7.挂载：main.js

router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})
